package com.example;

import com.example.domain.Users;
import com.example.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public class UserService {

    public static void main(String[] args)throws Exception {
        //初始化mybatis环境
        InputStream inputStream = null;
        SqlSession sqlSession=null;

        String resource = "mybatis-config.xml";
        inputStream = Resources.getResourceAsStream(resource);

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        sqlSession=sqlSessionFactory.openSession();


        //调用mapper完成业务

        UserMapper userMapper=sqlSession.getMapper(com.example.mapper.UserMapper.class);

        Users user=new Users();
        user.setUserId(1004);
        user.setUserName("happy");
        user.setUserSex(1);
        user.setPassword("123456");

        int rows=userMapper.insert(user);
        sqlSession.commit();
        System.out.println("rows:"+rows);

       // rows=userMapper.delete(1001);

       // System.out.println("rows:"+rows);
    }
}
